
import threading
from WorkBox import print_log
from WorkBox import socketinit
from WorkBox import readsocket
from WorkBox import send_data
from WorkBox import parse_json_and_store
from WorkBox import is_json
from WorkBox import write_responseinfo
from WorkBox import ST
from WorkBox import MAIsRun
from WorkBox import AlterVariable
import new_arch
import test_fun

import Task
#=====================数据处理 and 业务处理==================================



#执行上位机发来的的任务
def ExecuteTask(client_socket,data_dict):
    RequestType=data_dict.get("RequestType")   #获取到响应类型 参数信息
    if RequestType in MAIsRun:   #判断当前处理的任务是不是正在运行中
        #查询字典里面 当前机械臂是否运行
        if not MAIsRun.get(RequestType):
            #没有运行将改为运行状态  修改全局变量
            print_log("改为运行状态")
            AlterVariable(RequestType)
        else:
            responsedata=write_responseinfo(RequestType,"","inprogress\r\n")    #写入信息返还给客户端 
            send_data(client_socket,responsedata)
            client_socket.close()
            print_log(RequestType+"正在运行")
            return        #正在运行返回给客户端运行返回的信息
    #按照响应类型执行不同的任务
    if RequestType=="MechanicalArm_One":
        new_arch.robot_one()
    elif RequestType=="MechanicalArm_Two":
        new_arch.robot_two()
    elif RequestType=="MechanicalArm_Three":
        new_arch.robot_three(client_socket)
    elif RequestType=="photo_shoot":
        new_arch.photo_shoot()
    elif RequestType=="glass_process":
        new_arch.glass_process(client_socket)
    elif RequestType=="Stop":
         Task.Stop(client_socket)
    elif RequestType=="StopOne":
         Task.StopOne(client_socket)
    elif RequestType=="StopTwo":
         Task.StopTwo(client_socket)
    elif RequestType=="StopThree":
         Task.StopThree(client_socket)
    elif RequestType=="StopDetection":
         Task.StopDetection(client_socket)
    # if RequestType=="MechanicalArm_One":
    #     test_fun.fun1(client_socket)
    # elif RequestType=="MechanicalArm_Two":
    #     test_fun.fun2(client_socket)
    # elif RequestType=="MechanicalArm_Three":
    #     test_fun.fun3(client_socket)
    # elif RequestType=="Stop":
    #     Task.Stop(client_socket)
    # client_socket.close()

# 解析请求
def handle_client_connection(client_socket):
    #读出socket中的数据
    data=readsocket(client_socket)   
    #校验数据
    print_log("=====校验数据======")
    print_log(data)
    print_log("=====校验数据======")
    if not is_json(data):
        client_socket.close()   #不是json格式数据   关闭文件描述符 退出
        return 
    #将数据转换成字典数据
    data_dict=parse_json_and_store(data)
     
    client_thread = threading.Thread(target=ExecuteTask, args=(client_socket,data_dict))   #执行业务
    client_thread.start()
#====================================================================================================




#==================main funtion===========================================
def main_run(address="127.0.0.1",port=9999):
    server_socket=socketinit(address,port);      #init socket
    print_log("current thread_id:"+threading.current_thread().name)
    while True:
        # Accept a client connection
        client_socket, client_address = server_socket.accept()
        print_log(f"Connection from {client_address} has been established!")       
        handle_client_connection(client_socket)  #解析请求
#===========================================================================


main_run()




